<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>陪诊员登录</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-4">
            <h2 class="text-center mb-4">陪诊员登录</h2>
            <form id="escortLoginForm">
                <div class="mb-3">
                    <label for="phone" class="form-label">手机号</label>
                    <input type="tel" class="form-control" id="phone" pattern="1[3-9]\d{9}" required>
                </div>

                <div class="mb-3">
                    <label for="password" class="form-label">登录密码</label>
                    <input type="password" class="form-control" id="password" minlength="6" required>
                </div>

                <button type="submit" class="btn btn-primary w-100">立即登录</button>

                <div class="mt-3 text-center">
                    <a href="/escort/register.html">没有账号？立即注册</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    document.getElementById('escortLoginForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const phone = document.getElementById('phone').value;
        const password = document.getElementById('password').value;

        fetch(`http://localhost:8080/api/escort/login?phone=${encodeURIComponent(phone)}&password=${encodeURIComponent(password)}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        })
            .then(response => response.json())
            .then(data => {
                if (data.state === 2000) { // 成功
                    localStorage.setItem('escortToken', data.data.token);
                    localStorage.setItem('escortInfo', JSON.stringify(data.data));
                    window.location.href = '/escort/dashboard.html';
                } else {
                    alert('登录失败：' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('登录请求出错，请检查网络连接');
            });
    });
</script>
</body>
</html>
